package gameObjects;

import editor.Grid;
import editor.ObjContainer;
import editor.Menu.OGGETTI;
import gameObjects.Key;
import gameObjects.Pill;
import gameObjects.Ruby;
import java.io.IOException;
import java.util.Vector;
import logic.Coords2D;
import com.jme.scene.Node;

public class BuildBonus {
	private static BuildBonus	buildBonus;
	private Vector<Pill>		pillColl;
    private Vector<Ruby>		rubyColl;
    private Vector<Key>  		keyColl;
    private int					keys = 15;
	
	public static BuildBonus getInstance()
	{
		if(buildBonus==null)
			buildBonus=new BuildBonus();
		return buildBonus;
	}
	
	public Vector<Pill> getPills()
	{
		return pillColl;
	}
	
	public Vector<Ruby> getRubies()
	{
		return rubyColl;
	}
	
	public Vector<Key> getKeys()
	{
		return keyColl;
	}
	
	public Node create() throws IOException
    {
    	rubyColl = new Vector<Ruby>();
    	pillColl = new Vector<Pill>();
    	keyColl  = new Vector<Key>();
    	Node bonus = new Node("BONUS");
    	ObjContainer b;
    	
    	for(int i=0;i<Grid.objs.size();i++)
    	{
    		 b = new ObjContainer(Grid.objs.get(i).getCoords(),Grid.objs.get(i).getType());
    		 if (b.getType()==OGGETTI.RUBYBLUE)
    		 {
    			 rubyColl.add(new Ruby(OGGETTI.RUBYBLUE, b.getCoords()));
    			 bonus.attachChild(rubyColl.lastElement());
    		 }
    		 if (b.getType()==OGGETTI.RUBYGREEN) 
    		 {
    			 rubyColl.add(new Ruby(OGGETTI.RUBYGREEN, b.getCoords()));
    			 bonus.attachChild(rubyColl.lastElement());
    		 }
    		 if (b.getType()==OGGETTI.RUBYRED)
    		 {
    			 rubyColl.add(new Ruby(OGGETTI.RUBYRED, b.getCoords()));
    			 bonus.attachChild(rubyColl.lastElement());
    		 }
    		 if (b.getType()==OGGETTI.PILL)
    		 {
    			 pillColl.add(new Pill(b.getCoords()));
    			 bonus.attachChild(pillColl.lastElement());
    		 }
    	}
    	
    	int k = keys;
    	int pX=0;
    	int pY=0;
    	while(k>0)
    	{
    		pX =(int) (Math.random()*125)+1;
    		pY =(int) (Math.random()*125)+1;
    		
    		if (Grid.getCell(pX, pY)==OGGETTI.EMPTY)
    		{
    			keyColl.add(new Key(new Coords2D(pX, pY)));
    			Grid.objs.add(new ObjContainer(new Coords2D(pX, pY), OGGETTI.KEY));
    			Grid.setCell(pX, pY, OGGETTI.KEY);
    			bonus.attachChild(keyColl.lastElement());
    			k--;
    		}	
    	}
    	
    	return bonus;
    }
}
